<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
namespace app\services\care;

use app\services\BaseServices;
use app\dao\care\ServiceCateDao;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\facade\Db;
use think\facade\Route as Url;

class ServiceCateServices extends BaseServices
{

    public function __construct(ServiceCateDao $dao){
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param string $field
     * @param bool $is_page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where = [],$field = '*', bool $is_page = false){
        $page = $limit = 0;
        if($is_page) {
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$field,$page,$limit);
        foreach ($list['list'] as &$item) {
            $item['add_time'] = date('Y-m-d H:i',$item['add_time'] );
        }
        return $list;
    }

    /**
     * 添加表单
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id) {
        $info = $this->dao->get($id);
        if($info) {
            $title  = '修改';
        }else {
            $title = '添加';
        }
        $field = array();
        $field[] = Form::hidden('id',$info['id']??0);
        $field[] = Form::input('title','服务名称',$info['title']??'');
        $field[] = Form::frameImage('thumb','添加图标',Url::buildUrl('admin/widget.images/index',array('fodder'=>'thumb')),$info['thumb']??'')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide'=>true]);
        $field[] = Form::radio('is_recommend','是否推荐',$info['is_recommend']??1)->options([['value'=>1,'label'=>'推荐'],['value'=>0,'label'=>'不推荐']]);
        $field[] = Form::radio('status','是否显示',$info['status']??1)->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]);
        $field[] = Form::number('sort','排序',$info['sort']??0);
        return create_form($title, $field, Url::buildUrl('/care/cate/save'), 'POST');
    }

    /**
     * 保存
     * @param array $data
     * @return string
     */
    public function save(array $data=[]){
        $data['add_time'] = time();
        if(empty($data['id'])) {
            if (!$this->dao->save($data)) throw new AdminException('添加失败');
            return "添加成功";
        } else {
            if(!$this->dao->update($data['id'],$data)) throw new AdminException('修改失败');
            return "修改成功";
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public function del (int $id) {
        if($this->get($id)) return $this->dao->del((int)$id);
        throw new AdminException('删除失败');
    }

    /**
     * 是否推荐
     * @param int $id
     * @param int $is_recommend
     * @return string
     */
    public function is_recom(int $id,int $is_recommend) {
        $data = array(
            'id'=>$id,
            'is_recommend'=>$is_recommend
        );
        if($this->get($id)) {
            $this->dao->update($data['id'],$data);
            return "修改成功";
        }
        throw new AdminException('修改失败');
    }

    /**
     * 显示隐藏
     * @param int $id
     * @param int $status
     * @return string
     */
    public function status(int $id,int $status) {
        $data = array(
            'id'=>$id,
            'status'=>$status
        );
        if($this->get($id)) {
            $this->dao->update($id,$data);
            return "修改成功";
        }
        throw new AdminException('修改失败');
    }

    /**
     * 获取服务分类
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCateList(array $where = []){
        $model = Db::name('service_cate')->where(['is_del'=>0]);
        if(isset($where['is_recommend'])&&$where['is_recommend']!='') $model = $model->where('is_recommend',$where['is_recommend']);
        if(isset($where['status'])&&$where['status']!='') $model = $model->where('status',$where['status']);
        $cate = $model->order('sort desc,id desc')->select()->toArray();
        $list[] = ['value'=>'全部','label'=> ''];
        foreach ($cate as $item){
            $list[] = ['value'=>$item['title'],'label'=>$item['id']];
        }
        return $list;
    }
}
